//
//  TapAssetView.m
//  PingTu
//
//  Created by Yangfan on 15/2/4.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "TapAssetView.h"

@implementation TapAssetView

static UIImage *checkedIcon;
static UIColor *selectedColor;
static UIColor *disabledColor;
+ (void)initialize {
    checkedIcon     = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"ZYQAssetPicker.Bundle/Images/%@@2x.png",(!IS_IOS7) ? @"AssetsPickerChecked~iOS6" : @"AssetsPickerChecked"]]];
    selectedColor   = [UIColor colorWithWhite:1 alpha:0.3];
    disabledColor   = [UIColor colorWithWhite:1 alpha:0.9];
}

-(id)initWithFrame:(CGRect)frame {
    if (self=[super initWithFrame:frame]) {
        _selectView=[[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-checkedIcon.size.width, frame.size.height-checkedIcon.size.height, checkedIcon.size.width, checkedIcon.size.height)];
        [self addSubview:_selectView];
    }
    return self;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //    if (_disabled) {
    //        return;
    //    }
    //
    //    if (_delegate!=nil&&[_delegate respondsToSelector:@selector(shouldTap)]) {
    //        if (![_delegate shouldTap]&&!_selected) {
    //            return;
    //        }
    //    }
    
    //    if ((_selected=!_selected)) {
    //        self.backgroundColor=selectedColor;
    //        [_selectView setImage:checkedIcon];
    //    }
    //    else{
    //        self.backgroundColor=[UIColor clearColor];
    //        [_selectView setImage:nil];
    //    }
    if (_delegate!=nil&&[_delegate respondsToSelector:@selector(touchSelect:)]) {
        [_delegate touchSelect:YES];
    }
}

-(void)setDisabled:(BOOL)disabled {
    _disabled=disabled;
    if (_disabled) {
        self.backgroundColor=disabledColor;
    } else {
        self.backgroundColor=[UIColor clearColor];
    }
}

-(void)setSelected:(BOOL)selected{
    if (_disabled) {
        self.backgroundColor=disabledColor;
        [_selectView setImage:nil];
        return;
    }
    
    _selected=selected;
    if (_selected) {
        self.backgroundColor=selectedColor;
        [_selectView setImage:checkedIcon];
    }
    else{
        self.backgroundColor=[UIColor clearColor];
        [_selectView setImage:nil];
    }
}

@end